//
// Created by zhou on 2021/10/21.
//
struct ListNode{
    int val;
    ListNode *next;
    ListNode():val(0),next(nullptr){}
    ListNode(int x):val(x),next(nullptr){}
    ListNode(int x,ListNode *next):val(x),next(next){}
};
#include <iostream>
using namespace std;
class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *slow=new ListNode(0,head),*fast=new ListNode(0,head);
//        slow->next=head;
//        fast->next=head;
        for(int i=0;i<n;i++){
            fast=fast->next;
        }
        while(fast->next!= nullptr){
            fast=fast->next;
            slow=slow->next;
        }
        if(slow->next==head){
            head=head->next;
            slow->next=slow->next->next;
        }
        else{
            slow->next=slow->next->next;
        }
        return head;

    }
};
int main(){
    ListNode *p=new ListNode(1);
    ListNode*p1=new ListNode(2);
    ListNode *p2=new ListNode(7);
    ListNode *p3=new ListNode(3);

    ListNode *p4=new ListNode(4);
    ListNode *p5=new ListNode(5);
    p->next=p1;
    p1->next=p2;
    p2->next=p3;
    p3->next=p4;
    p4->next=p5;
    Solution solution;
    cout<<solution.removeNthFromEnd(p,6)->val;

}

